/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.dei.perla.parser.grammar.handlers;

import org.dei.perla.parser.datastructures.DataStructure;
import org.dei.perla.parser.datastructures.DataStructureFieldList;
import org.dei.perla.parser.datastructures.Field;
import org.dei.perla.parser.datastructures.Snapshot;
import org.dei.perla.parser.datastructures.Stream;
import org.dei.perla.parser.grammar.Token;
import org.dei.perla.parser.grammar.context.StatementContext;
import org.dei.perla.parser.util.Duration;
import org.dei.perla.parser.util.PairValue;

/**
 * 
 * Classe per gestire le strutture dati create tramite la grammatica
 *
 */
public class DataStructuresHandler {
	
	
	/**
	 * Inizializzazione della struttura dati
	 * @param table Struttura dati da inizializzare
	 */
	private static void setDataStructure(DataStructure table) {
		
		// Imposta il nome ed il flag isOutput prendendoli dal contesto corrente
		table.setName(StatementContext.currentStatementToken.image);
		table.setOutput(StatementContext.isOutput);
		
		// Aggiunta della DataStructure al sistema di tracking
		Helper.trackDataStructure(table, StatementContext.currentStatementToken);
		
		// Aggiunta alla DataStructure dei suoi Field parserizzati in precedenza 
		DataStructureFieldList fieldList = table.getFields();
		for(int i = 0; i < StatementContext.tokenDataStructureFieldList.size(); i++) {
			
			PairValue<Token, Field> value = StatementContext.tokenDataStructureFieldList.get(i);
			Field field = value.getSecond();
			
			// Aggiunta dell'oggetto Field al sistema di tacking
			Helper.trackField(table, field, value.getFirst());
			
			fieldList.add(field.getName(), field);
		}
	}
	
	/**
	 * Creazione dello Stream
	 */
	public static Stream createStream() {
		
		Stream stream = new Stream();
		setDataStructure(stream);
		return stream;
	}
	
	/**
	 * Creazione dello Snapshot
	 * @param duration Oggetto di tipo Duration associato allo Snapshot
	 * 
	 */
	public static Snapshot createSnapshot(Duration duration) {
		
		Snapshot snapshot = new Snapshot();
		setDataStructure(snapshot);
		snapshot.setDuration(duration);
		return snapshot;
	}
}
